package com.itheima.web;

import com.itheima.entity.Result;
import com.itheima.pojo.Goods;
import com.itheima.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController

public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    //查询所有
    @GetMapping("/goods")
    public Result findAll() {
        return this.goodsService.findAll();
    }

    @PostMapping("/add")
    //添加
    public Result add(@RequestBody Goods good) {
        return goodsService.add(good);
    }

    @GetMapping("/goods/search/{page}/{size}")
    //查询分页
    public Result findPage(@PathVariable("page") Integer page,
                           @PathVariable("size") Integer size) {
        return goodsService.findPage(page,size);
    }


    @GetMapping("/goods/{id}")
    //通过id查询
    public Result findById(@PathVariable("id") Integer id) {
        return goodsService.findById(id);
    }

    @PutMapping("/goods/{id}")
    //更新
    public Result update(@PathVariable("id")Integer id,
                         @RequestBody Goods goods) {

        return goodsService.update(id,goods);
    }


    @DeleteMapping("/goods/{id}")
    //删除
    public Result delete(@PathVariable("id") Integer id) {
        return goodsService.delete(id);
    }

}
